package com.hsy.linemoney.utils;

import android.util.SparseArray;

import com.hsy.linemoney.apiservice.LineMoneyContext;

import java.util.Locale;

/**
 * Created by MartinKent on 2016/10/26.
 */

public class NumberUtil {
    private static String[] str_nsim = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] str_sim = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static String[] ss_nsim = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"};
    private static String[] ss_sim = {"个", "十", "百", "千", "万", "十", "百", "千", "亿"};

    private static final SparseArray<String> numToChineseCache = new SparseArray<>();
    private static final SparseArray<String> numToEnglishCache = new SparseArray<>();

    public static String numToString(int number, boolean... isSimplefied) {
        if (Locale.CHINESE.equals(LineMoneyContext.getLocalLanguagePair().locale)) {
            String r = numToChineseCache.get(number);
            if (null == r) {
//                boolean b = (null == isSimplefied || 0 == isSimplefied.length) || isSimplefied[0];
//                String[] str = b ? str_sim : str_nsim;
//                String[] ss = b ? ss_sim : ss_nsim;
//                String s = String.valueOf(d);
//                StringBuffer sb = new StringBuffer();
//                for (int i = 0; i < s.length(); i++) {
//                    String index = String.valueOf(s.charAt(i));
//                    sb = sb.append(str[Integer.parseInt(index)]);
//                }
//                String sss = String.valueOf(sb);
//                int i = 0;
//                for (int j = sss.length(); j > 0; j--) {
//                    sb = sb.insert(j, ss[i++]);
//                }
//                r = sb.toString();
//                numToChineseCache.put(d, r);

                NumberText nt = NumberText.getInstance(NumberText.Lang.ChineseSimplified);
                r = nt.getText(number);
                numToChineseCache.put(number, r);
            }
            return r;
        } else {
            String r = numToEnglishCache.get(number);
            if (null == r) {
                NumberText nt = NumberText.getInstance(NumberText.Lang.English);
                r = nt.getText(number);
                numToEnglishCache.put(number, r);
            }
            return r;
        }

    }
}
